//
//  MPWebInfoViewController.m
//  MobileProject
//
//  Created by caiqilin on 2017/6/15.
//  Copyright © 2017年 caiqilin. All rights reserved.
//

#import "MPWebInfoViewController.h"
#import "MPInfoViewController.h"
#import "MPActivityViewController.h"
#import "MPNoticeViewController.h"
#import "MPTabBarController.h"
#import "AppDelegate.h"
@interface MPWebInfoViewController ()<UIScrollViewDelegate>
@property (nonatomic, weak) UIButton *selectedTitleButton;
@property (nonatomic, weak) UIView *indicatorView;
@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) UIView *titlesView;
@end

@implementation MPWebInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"官网资讯";
    
    [self setupChildViewControllers];
    
    [self setupScrollView];
    
    [self setupTitlesView];
    
    [self addChildVcView];
}

- (void)setupChildViewControllers {
    MPInfoViewController *baseInfo = [[MPInfoViewController alloc] init];
    
    [self addChildViewController:baseInfo];
    
    MPActivityViewController *detailInfo = [[MPActivityViewController alloc] init];

    [self addChildViewController:detailInfo];
    
    MPNoticeViewController *personInfo = [[MPNoticeViewController alloc] init];
    
    [self addChildViewController:personInfo];
}

- (void)setupScrollView {
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor whiteColor];
    
    scrollView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(self.childViewControllers.count * scrollView.ex_width, 0);
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
}

- (void)setupTitlesView {
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = [UIColor whiteColor];
    titlesView.frame = CGRectMake(0, 64, self.view.ex_width, 40);
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;
    
    NSArray *titles = @[@"资讯", @"活动", @"公告"];
    NSUInteger count = titles.count;
    CGFloat titleButtonW = titlesView.ex_width / count;
    CGFloat titleButtonH = titlesView.ex_height;
    for (NSUInteger i = 0; i < count; i++) {
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        titleButton.tag = i;
        titleButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [titlesView addSubview:titleButton];
        
        [titleButton setTitle:titles[i] forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [titleButton setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        
        titleButton.frame = CGRectMake(i * titleButtonW, 0, titleButtonW, titleButtonH);
    }
    
    UIButton *firstTitleButton = titlesView.subviews.firstObject;
    
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [firstTitleButton titleColorForState:UIControlStateSelected];
    indicatorView.ex_height = 1;
    indicatorView.ex_y = titlesView.ex_height - indicatorView.ex_height;
    [titlesView addSubview:indicatorView];
    self.indicatorView = indicatorView;
    
    [firstTitleButton.titleLabel sizeToFit];
    indicatorView.ex_width = firstTitleButton.titleLabel.ex_width;
    indicatorView.ex_centerX = firstTitleButton.ex_centerX;
    
    firstTitleButton.selected = YES;
    self.selectedTitleButton = firstTitleButton;
}

- (void)titleClick:(UIButton *)titleButton
{
    
    self.selectedTitleButton.selected = NO;
    titleButton.selected = YES;
    self.selectedTitleButton = titleButton;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.ex_width = titleButton.titleLabel.ex_width;
        self.indicatorView.ex_centerX = titleButton.ex_centerX;
    }];
    
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = titleButton.tag * self.scrollView.ex_width;
    [self.scrollView setContentOffset:offset animated:YES];
}

- (void)addChildVcView {
    NSUInteger index = self.scrollView.contentOffset.x / self.scrollView.ex_width;
    
    UIViewController *childVc = self.childViewControllers[index];
    if ([childVc isViewLoaded]) return;
    
    childVc.view.frame = self.scrollView.bounds;
    [self.scrollView addSubview:childVc.view];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self addChildVcView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSUInteger index = scrollView.contentOffset.x / scrollView.ex_width;
    UIButton *titleButton = self.titlesView.subviews[index];
    [self titleClick:titleButton];
    
    [self addChildVcView];
}

@end
